import TextureRenderUtils from "../TextureRenderUtils";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ColorPanelV2 extends cc.Component {

    @property([cc.Component.EventHandler])
    callback: cc.Component.EventHandler[] = [];

    protected colors: cc.Color[] = []

    @property(cc.Camera)
    camera: cc.Camera = null;

    @property(cc.Sprite)
    target: cc.Sprite = null;

    @property(cc.Node)
    renderNode: cc.Node = null;

    protected textureHelper: TextureRenderUtils = new TextureRenderUtils()

    start() {
        this.node.on(cc.Node.EventType.TOUCH_START, this.touchStart, this)
        this.init();
    }

    init() {

        this.textureHelper.init(this.camera, this.renderNode)

        this.textureHelper.render()

        let data = this.textureHelper.getData()

        if (data.length > 0) {
            // cc.log(' data 00?000 ',data)
            for (let index = 0; index < data.length; index += 4) {
                this.colors.push(cc.color(data[index], data[index + 1], data[index + 2], data[index + 3]))
            }
            // cc.log(' colors ',this.colors)
            cc.log(' width ', this.node.width, ' height ', this.node.height)
            cc.log(' 实际上有多少个点  == ', data.length / 4)
            let count = this.node.width * this.node.height;
            cc.log(" 应该有多少个点的颜色 ", count)
            if (this.target) {
                let tTexture = this.target.spriteFrame.getTexture()
                tTexture.setFlipY(false)
                this.target.node.scaleY = -1
                tTexture.initWithData(data, tTexture.getPixelFormat(), this.node.width, this.node.height)
                
            }
        }

    }


    touchStart(e: cc.Touch) {
        let pos = e.getLocation();
        // pos = this.node.convertToWorldSpaceAR(pos)
        pos = this.node.convertToNodeSpaceAR(pos)
        // pos = this.node.parent.convertToWorldSpaceAR(pos)
        // let worldpos = this.node.convertToWorldSpaceAR(cc.v2())
        // console.log(' pos ', pos.x, pos.y, ' worldpos', worldpos.x, worldpos.y)
        let width = Math.floor(this.node.width)//this.node.width;
        let height = Math.floor(this.textureHelper.height)// this.node.height;
        // let x = Math.floor(pos.x + worldpos.x)
        // let y = Math.floor(pos.y + worldpos.y)

        let x = Math.floor(pos.x)
        let y = Math.floor(pos.y)

        cc.log(' x ', x, ' y = ', y, ' this.renderNode.width ', width)
        let index = x + width * y
        index = Math.floor(index)
        cc.log(" index ", index)
        // let data = this.textureHelper.readPixels(null, x, y, 1, 1)
        // console.log(' color is ', data)
        // let color = cc.color(data[0], data[1], data[2], data[3])
        let color = this.colors[index];
        if (color.getA() == 0) {
            console.log(' a == 0 ')
            return;
        }
        if (color.a != 255) {
            cc.log(' color.a ', color.a)
            color.a = 255;
        }        
        for (let index = 0; index < this.callback.length; index++) {
            const callback = this.callback[index];
            callback.emit([color])
        }
    }

    // update (dt) {}
}
